package com.hlf.modules.api.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.entity.CartEntity;
import com.hlf.modules.api.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 购物车
 * Created by Administrator on 2017/10/9.
 */

@RestController
@RequestMapping("/api/cart")
@Api("购物车控制接口")
public class ApiCartController {

    @Autowired
    private CartService cartService;


    /**
     * 添加购物车/修改数量
     */
    @SysLog("添加/修改购物车")
    @PostMapping("create")
    @ApiOperation(value = "添加购物车/修改数量", notes = "添加购物车/修改数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "goodsId", value = "商品id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "goodsNum", value = "商品数量", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult createCart(String goodsId, String goodsNum, String tableId, HttpServletRequest request) {
        Assert.isBlank(tableId, "餐桌id不能为空");
        Assert.isBlank(goodsId, "商品id不能为空");
        Assert.isBlank(goodsNum, "商品数量不能为空");

        cartService.createCart(goodsId, goodsNum, tableId, request);
        return ReturnResult.ok();
    }

    /**
     * 购物车列表
     */
    @GetMapping("cartList/{tableId}")
    @ApiOperation(value = "购物车列表", notes = "购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult updateGoodsNum(@PathVariable("tableId") String tableId) {
        Assert.isBlank(tableId, "餐桌id不能为空");

        List<CartEntity> cartEntityList = cartService.queryCartListByTableId(Long.parseLong(tableId));
        cartEntityList = cartEntityList.stream().filter(cartEntity -> cartEntity.getGoodsTypeId() != 1).collect(Collectors.toList());
        Map<String, Object> map = new HashedMap();
        map.put(Constants.DEFAULT_RESULT_NAME, cartEntityList);
        return ReturnResult.ok(map);
    }

    /**
     * 清空购物车
     */
    @SysLog("清空购物车")
    @GetMapping("delCart/{tableId}")
    @ApiOperation(value = "清空购物车", notes = "清空购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult cleanCart(@PathVariable("tableId") String tableId) {
        Assert.isBlank(tableId, "餐桌id不能为空");

        cartService.deleteCartByTableId(Long.parseLong(tableId));
        return ReturnResult.ok();
    }


}
